<?php

namespace app\common\model;
//聊天记录表模型
class MsgList extends BaseModel
{
    protected $table = 'msg_list';

    function getTypeAttr($val)
    {
        $arr = [0 => '单聊', 1 => '群聊', 2 => '系统'];
        return $arr[$val];
    }

    function getIsReadAttr($val)
    {
        $arr = [0 => '未读', 1 => '已读'];
        return $arr[$val];
    }

    public function getList($GroupId,$page,$num)
    {
        $list = $this
        ->where('group_id',$GroupId)
        ->limit($page,$num)
        ->order('create_time DES')
        ->select();
   
        //然后遍历数组查询每个发消息的人个人资料
        foreach ($list as $key => $value) {
            $froms = (new Member)->where('user_name',$value['from'])->field('user_name,nick_name,avatar_url')->find();
            $tos = (new Member)->where('user_name',$value['to'])->field('user_name,nick_name,avatar_url')->find();
            $list[$key]['fromInfo'] = $froms;
            $list[$key]['toInfo'] = $tos;
        }
        return $list;

    }

}
